// fast4j - Fast & Agile Service Tools for Java
// Copyright (C) 2007 Alexandre ROMAN
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

// $Id: ApplicationDescriptorPropertyConverter.java 51 2007-04-08 12:37:12Z alexandre.roman $

package fast4j.ace;

import java.util.HashMap;
import java.util.Map;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
 * XStream application property converter.
 * @author alexandre.roman
 */
class ApplicationDescriptorPropertyConverter implements Converter {
    public boolean canConvert(Class type) {
        return Map.class.isAssignableFrom(type);
    }
    
    public void marshal(Object source, HierarchicalStreamWriter writer,
            MarshallingContext ctx) {
    }
    
    public Object unmarshal(HierarchicalStreamReader reader,
            UnmarshallingContext ctx) {
        final Map<String, Object> props = new HashMap<String, Object>();
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            
            final String key = reader.getNodeName();
            final Object value = reader.getValue();
            props.put(key, value);
            
            reader.moveUp();
        }
        
        return props;
    }
}
